//.pragma library


function readData(key) {
    var ret = undefined;
    db().transaction(
        function(tx) {
            createTable(tx);

            var rs = tx.executeSql('SELECT value FROM Settings WHERE key=?', key);

            if (rs.rows.length > 0) {
                ret = rs.rows.item(0).value;
            }
        }
    )
    return ret;
}

function saveData(key, value) {
    db().transaction(
        function(tx) {
            createTable(tx);

            var currentValue = readData(key);
            if (currentValue != value) {
                if (currentValue !== undefined) {
                    tx.executeSql('UPDATE Settings SET value = ? WHERE key = ?', [ value, key ]);
                } else {
                    tx.executeSql('INSERT INTO Settings VALUES(?, ?)', [ key, value ]);
                }
            }
        }
    )
}

function db() {
    return openDatabaseSync("JCTHEMESIMULATOR", "1.0", "Settings storage for JcThemeSimulator", 10000);
}

function createTable(tx) {
    tx.executeSql('CREATE TABLE IF NOT EXISTS Settings(key TEXT, value TEXT)');
}
